package project.controller;

import project.entity.Student;
import project.service.StudentService;
import project.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
杨威
2021/9/16
14:00
*/
@WebServlet("/update")
public class UpdateStudent extends HttpServlet {
    StudentService studentService=new StudentServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String stuId = request.getParameter("stuId");
        String stuName = request.getParameter("stuName");
        String stuGender = request.getParameter("stuGender");
        String stuPhone = request.getParameter("stuPhone");
        String bornDate = request.getParameter("bornDate");
        String stuEmail = request.getParameter("stuEmail");
        String stuAddress = request.getParameter("stuAddress");
        String gradeId = request.getParameter("gradeId");
        Date parse=null;
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
            parse = sdf.parse(bornDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Student student=new Student(Integer.valueOf(stuId),stuName,stuGender,stuPhone,parse,stuEmail,stuAddress,Integer.valueOf(gradeId));
        int i = studentService.updateStudent(student);
        if(i>0){
            response.sendRedirect(request.getContextPath()+"/showAllStudentController");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
